import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QDialog
import sys

from get_config import *
from ClientUi.deviceManager.dev_config import global_camera_trigger_mode, global_camera_rotate, max_camera_num

class Ui_camera_params_setting(object):
    def setupUi(self, camera_params_setting):
        camera_params_setting.setObjectName("camera_params_setting")
        camera_params_setting.setFixedSize(314, 360)

        self.label = QtWidgets.QLabel(camera_params_setting)
        self.label.setGeometry(QtCore.QRect(10, 10, 81, 21))
        self.label.setObjectName("label")
        self.cameraname_lb = QtWidgets.QLabel(camera_params_setting)
        self.cameraname_lb.setGeometry(QtCore.QRect(70, 10, 72, 21))
        self.cameraname_lb.setObjectName("cameraname_lb")

        self.ok_btn = QtWidgets.QPushButton(camera_params_setting)
        self.ok_btn.setGeometry(QtCore.QRect(222, 300, 80, 36))
        self.ok_btn.setObjectName("ok_btn")

        self.widget = QtWidgets.QWidget(camera_params_setting)
        self.widget.setGeometry(QtCore.QRect(10, 60, 291, 220))
        self.widget.setObjectName("widget")
        self.formLayout = QtWidgets.QFormLayout(self.widget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.label_82 = QtWidgets.QLabel(self.widget)
        self.label_82.setObjectName("label_82")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_82)
        self.camtriggermode_cb = QtWidgets.QComboBox(self.widget)
        self.camtriggermode_cb.setObjectName("camtriggermode_cb")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.camtriggermode_cb)
        self.label_3 = QtWidgets.QLabel(self.widget)
        self.label_3.setObjectName("label_3")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_3)
        self.camchangesize_checkb = QtWidgets.QCheckBox(self.widget)
        self.camchangesize_checkb.setObjectName("camchangesize_checkb")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.camchangesize_checkb)
        self.label_4 = QtWidgets.QLabel(self.widget)
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_4)
        self.camimagewidth = QtWidgets.QLineEdit(self.widget)
        self.camimagewidth.setObjectName("camimagewidth")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.camimagewidth)
        self.label_5 = QtWidgets.QLabel(self.widget)
        self.label_5.setObjectName("label_5")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_5)
        self.camimageheight = QtWidgets.QLineEdit(self.widget)
        self.camimageheight.setObjectName("camimageheight")
        self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.camimageheight)
        self.label_6 = QtWidgets.QLabel(self.widget)
        self.label_6.setObjectName("label_6")
        self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_6)
        self.camrotate_cb = QtWidgets.QComboBox(self.widget)
        self.camrotate_cb.setObjectName("camrotate_cb")
        self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.camrotate_cb)        
        self.retranslateUi(camera_params_setting)
        QtCore.QMetaObject.connectSlotsByName(camera_params_setting)


    def retranslateUi(self, camera_params_setting):
        _translate = QtCore.QCoreApplication.translate
        camera_params_setting.setWindowTitle(_translate("camera_params_setting", "配置相机"))
        self.label.setText(_translate("camera_params_setting", "当前相机:"))
        self.cameraname_lb.setText(_translate("camera_params_setting", "相机1"))
        self.ok_btn.setText(_translate("camera_params_setting", "保存"))
        self.label_82.setText(_translate("camera_params_setting", "触发方式:"))
        self.label_3.setText(_translate("camera_params_setting", "尺寸变换:"))
        self.camchangesize_checkb.setText(_translate("camera_params_setting", "变换图像尺寸"))
        self.label_4.setText(_translate("camera_params_setting", "图像宽度:"))
        self.label_5.setText(_translate("camera_params_setting", "图像高度:"))
        self.label_6.setText(_translate("camera_params_setting", "旋转角度:"))
        
class camera_params_setting(QDialog, Ui_camera_params_setting):
    def __init__(self,camidx = -1,parent=None):
        super(camera_params_setting,self).__init__(parent)
        currentPath = os.path.abspath(__file__)
        path = os.path.dirname(currentPath)
        clientUiPath = os.path.dirname(path)
        self.setWindowIcon(QIcon(f'{clientUiPath}\\images\\icon.ico'))  # 设置窗口图标
        self.setWindowTitle('相机参数配置')
        self.setupUi(self)
        self.camidx = camidx
        self.camtriggermode_cb.addItems(global_camera_trigger_mode)
        self.camrotate_cb.addItems(global_camera_rotate)
        self.ok_btn.clicked.connect(self.updateparams)
        self.init_data()
        # 禁用最大化按钮
        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)
        self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)

    def init_data(self):
        if self.camidx >= 0 and self.camidx < max_camera_num:
            self.cameraname_lb.setText(f'相机{self.camidx+1}')
            self.camtriggermode_cb.setCurrentIndex(config.get(f"camera{self.camidx+1}_hardtrigger", 0))
            self.camchangesize_checkb.setChecked(config.get(f"camera{self.camidx+1}_resize", 0))
            self.camimageheight.setText(str(config.get(f"camera{self.camidx+1}_resizeimgheight", 0)))
            self.camimagewidth.setText(str(config.get(f"camera{self.camidx+1}_resizeimgwidth", 0)))
            self.camrotate_cb.setCurrentText(config.get(f"camera{self.camidx+1}_rotatedisplay", ''))
        else:
            self.cameraname_lb.setText(f'---')

    def set_camera_id(self,idx):
        self.camidx = idx
        self.init_data()

    def updateparams(self):
        if self.camidx >= 0 and self.camidx < max_camera_num:
            config[f"camera{self.camidx+1}_hardtrigger"] = self.camtriggermode_cb.currentIndex()
            config[f"camera{self.camidx+1}_resize"] = self.camchangesize_checkb.isChecked()
            config[f"camera{self.camidx+1}_resizeimgheight"] = self.camimageheight.text()
            config[f"camera{self.camidx+1}_resizeimgwidth"] = self.camimagewidth.text()
            config[f"camera{self.camidx+1}_rotatedisplay"] = self.camrotate_cb.currentText()
            config_save()
        self.accept()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    try:
        current_path = os.path.abspath(__file__)
        path = os.path.dirname(current_path)
        path = os.path.dirname(path)
        window = camera_params_setting()
        app.setStyleSheet(open(f'{path}/styleSheet.qss', encoding='utf-8').read())
        window.showNormal()
        sys.exit(app.exec_())

    except Exception as e:
        print(e)     